package com.ciwong.epaper.modules.reciteWords.dbreciteword;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.ciwong.epaper.common.db.DatabaseHelper;
import com.ciwong.libs.utils.CWLog;

import java.util.ArrayList;
import java.util.List;

public class WordDetailDao {


    private String TAG = "WordDetailDao";
    private SQLiteDatabase db;

    public WordDetailDao() {
        this.db = DatabaseHelper.getDatabase();
    }

    public void creatTable(String tableName) {
        CWLog.d(TAG, "creatTable 创建数据库表");
        db.execSQL(DBColumns.getCreateTableWordDetailString(tableName));
    }

    public void dropTable(String tableName) {
        String sql = "DROP TABLE IF EXISTS " + tableName;
        db.execSQL(sql);
    }

    public boolean checkTableIsExist(String tableName) {
        boolean result = false;
        String sql = "select count(*) as c from Sqlite_master  where type ='table' and name =" + "'" + tableName.trim() + "';";
        try {
            Cursor cursor = db.rawQuery(sql, null);
            if (cursor.moveToNext()) {
                int count = cursor.getInt(0);
                if (count > 0) {
                    result = true;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public SQLiteDatabase getDb() {
        return db;
    }

    /**
     * 添加数据到数据库对应表中
     *
     * @param tableName
     * @param dId
     * @param wId
     * @param words
     * @param wordFile
     * @param wordType
     * @param symbol
     * @param syllable
     * @param pretations
     * @param sentences
     * @param sentFile
     * @param wordPic
     * @param color
     * @return
     */
    public boolean add(String tableName, String dId,
                       String wId,
                       String words,
                       String wordFile,
                       int wordType,
                       String symbol,
                       String syllable,
                       String pretations,
                       String sentences,
                       String sentFile,
                       String wordPic,
                       int color) {
        ContentValues values = new ContentValues();
        values.put(DBColumns.EXPLANATION_ID, dId);
        values.put(DBColumns.WORD_ID, wId);
        values.put(DBColumns.WORDS, words);
        values.put(DBColumns.WORD_FILE, wordFile);
        values.put(DBColumns.WORD_TYPE, wordType);
        values.put(DBColumns.SYMBOL, symbol);
        values.put(DBColumns.SYLLABLE, syllable);
        values.put(DBColumns.PRETATIONS, pretations);
        values.put(DBColumns.SENTENCE, sentences);
        values.put(DBColumns.SENTENCE_FILE, sentFile);
        values.put(DBColumns.WORD_PIC, wordPic);
        values.put(DBColumns.COLOR, color);
//        long result = db.insert(tableName, null, values);
        long result = db.replace(tableName, null, values);
        return result != -1;
    }

    /**
     * 监测数据库中是否存在对应 id数据
     *
     * @param tableName
     * @param id
     * @return
     */
    public boolean queryIsExist(String tableName, String id) {

        Cursor cursor = db.query(tableName, null, DBColumns._ID + "=?", new String[]{id}, null, null, null);
        if (cursor.moveToNext()) {
            cursor.close();
            return true;
        }
        cursor.close();
        return false;
    }


    public boolean delete(String tableName, String dbId) {
        int id = db.delete(tableName, DBColumns.EXPLANATION_ID + "=?", new String[]{dbId});
        return id != -1;
    }

    public boolean deleteAll(String tableName) {
        int id = db.delete(tableName, null, null);
        return id != -1;
    }

    public int getRowCount(String tableName) {
        return db.query(tableName, null, null, null, null, null, null).getCount();
    }
}
